/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */
package cn.com.ksplatform.modules.autocode.global.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;

import org.apache.velocity.app.VelocityEngine;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.hibernate.engine.jdbc.internal.FormatStyle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.ui.velocity.VelocityEngineUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;

import cn.com.ksplatform.base.Return;
import cn.com.ksplatform.base.SimpleReturn;
import cn.com.ksplatform.base.dao.IBaseJdbcDAO;
import cn.com.ksplatform.common.logger.Log;
import cn.com.ksplatform.modules.autocode.dto.Column;
import cn.com.ksplatform.modules.autocode.dto.ColumnArray;
import cn.com.ksplatform.modules.autocode.manager.model.Sence;
import cn.com.ksplatform.modules.autocode.util.SenceUtil;

/**
 * ClassName:Adapter_0_update.java <br/>
 * Date: 2015年11月26日 <br/>
 * 
 * @author Mr.Xiao
 * @version 1.0
 */
@Component
public class Adapter_0_update implements IMethodAdapter{
	@Autowired
	@Qualifier("baseJdbcDao")
	private IBaseJdbcDAO jdbcDao;
	
	@Override
	@Transactional
	public void doAdapter(VelocityConfigurer config, 
			Sence senceModel,  HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ColumnArray columns = SenceUtil.getColumns(senceModel);
		VelocityEngine ve = config.getVelocityEngine();
		Map param = new HashMap();
		param.put("table",senceModel.getPropertys().get("table"));
		
		Map<String,List<Column>> cls = getQueryAndList(columns);
		param.putAll(cls);
		
		String sql = VelocityEngineUtils.mergeTemplateIntoString(ve,"cn/com/ksplatform/modules/autocode/template/sql/base/update.vm","UTF-8",param);
		sql = FormatStyle.BASIC.getFormatter().format(sql);
		
		List paramList = new ArrayList();
		for(Column c : cls.get("form")){
			String v = request.getParameter(c.getName());
			if(StringUtils.hasLength(v)){
				paramList.add(v);
			}else{
				paramList.add(null);
			}
		}
		for(Column c : cls.get("pk")){
			String v = request.getParameter(c.getName());
			if(StringUtils.hasLength(v)){
				paramList.add(v);
			}else{
				paramList.add(null);
			}
		}
		
		int len = jdbcDao.update(sql, paramList.toArray());
		Log.info(sql);
		ObjectMapper mapper = new ObjectMapper();
		
		Return r = null;
		if(len > 0){
			r = SimpleReturn.success("更新成功");
		}else{
			r = SimpleReturn.success("更新失败");
		}
		
		String outHtml = mapper.writeValueAsString(r);
		response.setContentType("application/json");
		response.getWriter().write(outHtml);
	}
	
	private Map<String,List<Column>> getQueryAndList(ColumnArray columnArray){
		List<Column> pks = new ArrayList<Column>();
		List<Column> forms = new ArrayList<Column>();
		for(Column c : columnArray.getColumns()){
			if(c.getPropertys() != null){
				if(c.getPk()){
					//主键
					pks.add(c);
				}
				
				if("true".equals(c.getPropertys().get("formShow")) && !c.getPk()){
					forms.add(c);
				}
			}
		}
		Map m = new HashMap();
		m.put("pk", pks);
		m.put("form", forms);
		return m;
	}
}
